﻿/**
 * Copyright (C) 2011 original author and authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using NGuice.Inject.Internal.Util;
using NGuice.Inject.Internal;

namespace NGuice.Inject.Spi
{
    //complete!
    /// <summary>
    /// 一个错误消息是程序运行的一个上下文。消息通常内部被NGuice或其扩展闯进啊。消息也能显式的在模块中
    /// 使用Binder.AddError(Exception)创建：
    /// <code>
    /// try {
    ///    BindPropertiesFromFile();
    ///} catch (IOException e) {
    ///    AddError(e);
    ///}
    /// </code>
    /// </summary>
    [Serializable]
    public sealed class Message : IElement
    {
        private readonly string message;

        private readonly Exception cause;

        private readonly IList<object> sources;

        public Message(IList<object> sources, string message, Exception cause)
        {
            this.message = Preconditions.CheckNotNull(message, "message");
            this.sources = ImmutableList<object>.CopyOf<object>(sources);
            this.cause = cause;
        }

        public Message(object source, string message)
            : this(ImmutableList<object>.Of(source), message, null)
        {
        }

        public Message(string message)
            : this(ImmutableList<object>.Of(), message, null)
        {
        }

        public string Source
        {
            get
            {
                return sources.Count == 0 ? SourceProvider.UNKNOWN_SOURCE.ToString() : Errors.Convert(sources[sources.Count - 1]).ToString();
            }
        }

        /// <summary>
        /// 错误消息内容
        /// </summary>
        public string Content
        {
            get
            {
                return message;
            }
        }

        public Exception Cause
        {
            get
            {
                return cause;
            }
        }

        public override string ToString()
        {
            return message;
        }

        public override int GetHashCode()
        {
            return message.GetHashCode();
        }

        public override bool Equals(object o)
        {
            if (!(o is Message))
            {
                return false;
            }
            Message e = (Message)o;
            return message.Equals(e.message)
                      && (cause == e.Cause || (cause != null && cause.Equals(e.Cause)))
                      && sources.Equals(e.sources);
        }

        private object WriteReplace()
        {
            object[] sourcesAsStrings = sources.ToArray();
            for (int i = 0; i < sourcesAsStrings.Length; i++)
            {
                sourcesAsStrings[i] = Errors.Convert(sourcesAsStrings[i]).ToString();
            }
            return new Message(sourcesAsStrings.ToList(), message, cause);
        }

        public IList<object> Sources
        {
            get { return sources; }
        }

        public T AcceptVisitor<T>(IElementVisitor<T> visitor)
        {
            return visitor.Visit(this);
        }

        public void ApplyTo(IBinder binder)
        {
            binder.WithSource(Source).AddError(this);
        }

        object IElement.Source
        {
            get { return Source; }
        }
    }
}
